<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="客户编码" prop="code">
          <el-input :style="{width: '120px'}" v-model="queryParams.code" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户名称" prop="name">
          <el-input :style="{width: '120px'}" v-model="queryParams.name" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input :style="{width: '120px'}" v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="归属团队" prop="team_id">
          <el-input :style="{width: '120px'}" v-model="queryParams.team_id" placeholder="请输入归属团队" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bd:partner:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['bd:partner:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bd:partner:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['bd:partner:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" width="150px" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="固定电话" align="center" prop="telephone" />
      <el-table-column label="邮箱地址" align="center" prop="email" />
      <el-table-column label="是否启用" align="center" prop="enable_flag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enable_flag" />
        </template>
      </el-table-column>
      <el-table-column label="客户编码" align="center" prop="code">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)"
            v-hasPermi="['bd:partner:query']">{{scope.row.code}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-info" @click="gotoLead(scope.row)"
              v-hasPermi="['bd:partner:edit']">跟单
          </el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bd:partner:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bd:partner:remove']">删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="update_by" />
      <el-table-column label="更新时间" align="center" prop="update_time">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.update_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入客户编码" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag" active-color="#13ce66" active-text="自动生成"
                @change="handleAutoGenChange(autoGenFlag)" v-if="optType != 'view'">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="归属销售" prop="user_id">
              <el-select v-model="queryParams.user_id" placeholder="请输入归属销售" clearable>
                <el-option v-for="dict in dict.type.crm_sales" :key="dict.value" :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="归属团队" prop="team_id">
              <el-input v-model="form.team_id" placeholder="请输入归属团队" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入地址内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="网站地址" prop="website">
              <el-input v-model="form.website" placeholder="请输入网站地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" v-if="optType=='view'" />
              <el-input v-model="form.email" placeholder="请输入邮箱地址" v-else />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="固定电话" prop="telephone">
              <el-input v-model="form.telephone" v-if="optType=='view'" />
              <el-input v-model="form.telephone" placeholder="请输入固定电话" v-else />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标识LOGO" prop="logo">
              <el-input v-model="form.logo" v-if="optType=='view'" />
              <el-input v-model="form.logo" placeholder="请输入标识LOGO" v-else />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="生日或成立日期" prop="birthday">
              <el-date-picker v-model="form.birthday" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                :style="{width: '100%'}" placeholder="请选择生日或成立日期" clearable></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码或身份证号" prop="id_card">
              <el-input v-model="form.id_card" v-if="optType=='view'" />
              <el-input v-model="form.id_card" placeholder="请输入信用编码或身份证号" v-else />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="评分" prop="score">
              <el-input v-model="form.score" readonly v-if="optType=='view'" />
              <el-input v-model="form.score" placeholder="请输入评分" v-else />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否为客户" prop="is_customer">
              <el-switch v-model="form.is_customer" active-value='0' inactive-value='1'></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否为供应商" prop="is_supplier">
              <el-switch v-model="form.is_supplier" active-value='0' inactive-value='1'></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否启用" prop="enable_flag">
              <el-switch v-model="form.enable_flag" active-value='0' inactive-value='1'></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" readonly v-if="optType=='view'" />
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" v-else />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPartner,
    getPartner,
    delPartner,
    addPartner,
    updatePartner
    // takePartner,
    // PutPrivilegePartner
  } from "../api/partner";
  import {
    genCode
  } from "@/api/system/autocode/rule";
  import store from '@/store';
  export default {
    name: "Partner",
    dicts: ['crm_sales', 'crm_product', 'sys_yes_no'],
    data() {
      return {
        //自动生成编码
        autoGenFlag: false,
        optType: undefined,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户表格数据
        partnerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          code: null,
          name: null,
          user_id: store.getters.user_id,
          team_id: null,
          mobile: null,
          birthday: null,
          id_card: null,
          score: null,
          enable_flag: null,
          is_customer: null,
          is_supplier: null
        },
        // user_id: null,
        // user_ids: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "客户名称不能为空",
            trigger: "blur"
          }],
          email: [{
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }],
          mobile: [{
            required: true,
            message: "手机号不能为空",
            trigger: "blur"
          }],
          enable_flag: [{
            required: true,
            message: "是否启用不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.queryParams = this.$route.query; //传递路由上的参数进行分类查询
      this.getList();
    },
    methods: {
      /** 查询客户列表 */
      getList() {
        this.loading = true;
        if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
          this.queryParams["beginUpdateTime"] = this.daterangeUpdateTime[0];
          this.queryParams["endUpdateTime"] = this.daterangeUpdateTime[1];
        }
        listPartner(this.queryParams).then(response => {
          this.partnerList = response.rows;
          this.total = response.total;
          // this.queryParams.sales = response.rows[0].sales
          this.loading = false;
        });
      },
      // submitPrivilege() {
      //   PutPrivilegePartner(this.user_ids, this.user_id).then(response => {
      //     this.openPrivilege = false
      //     this.getList()
      //   })
      // },
      // 取消按钮
      cancel() {
        this.open = false;
        this.openPrivilege = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          code: null,
          name: null,
          user_id: store.getters.user_id,
          team_id: null,
          address: null,
          mobile: null,
          website: null,
          email: null,
          telephone: null,
          logo: null,
          birthday: null,
          score: null,
          enable_flag: null,
          remark: null,
          create_time: null,
          update_by: null,
          update_time: null,
          id_card: null,
          is_customer: null,
          is_supplier: null
        };
        this.autoGenFlag = false;
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户";
        this.optType = "add";
      },
      // 查询明细按钮操作
      handleView(row) {
        this.reset();
        const id = row.id || this.ids
        getPartner(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看客户";
          this.optType = "view";
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPartner(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户";
          this.optType = "edit";
        });
      },
      /** 提交按钮 */
      submitForm() {
        //console.log("12222");
        this.$refs["form"].validate(valid => {
          //console.log("12222");
          if (valid) {
            if (this.form.id != null) {
              updatePartner(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              console.log("12222");
              addPartner(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除客户ids为"' + ids + '"的数据项？').then(function() {
          return delPartner(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      // handleExport() {
      //   this.download('md/client/export', {
      //     ...this.queryParams
      //   }, `client_${new Date().getTime()}.xlsx`)
      // },
      //自动生成编码
      handleAutoGenChange(autoGenFlag) {
        debugger;
        if (autoGenFlag) {
          genCode('PARTNER_CODE').then(response => {
            this.form.code = response;
          });
        } else {
          this.form.code = null;
        }
      }
    }
  };
</script>
<!-- -->
